const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

// 模拟的用户列表
const users = [
    { id: 1, username: 'user1', password: 'pass1' },
    { id: 2, username: 'user2', password: 'pass2' },
];

app.use((req, res, next) => {
    const user = users.find((user) => user.username === req.body.username && user.password === req.body.password);

    if (!user) {
        return res.status(401).json({ message: 'Invalid username or password' });
    }

    req.user = user;
    next();
});

app.post('/login', (req, res) => {
    res.json({ message: 'Login successful', user: req.user });
});

app.listen(port, () => {
    console.log(`Server is running at ***${port}`);
});
